#include "oled.h"
#include "spi.h"
#include "systick.h"
#include "color.h"

u16 FRONT_COLOR;	//<画笔颜色
u16 BACK_COLOR;  //<背景色
OLED_INFO oled_info;	//<OLED基本信息

/*****************************************************************************
 * @name       :void Set_X(u16 x)
 * @date       :2021.6.6 
 * @function   :设置x坐标
 * @param 		 :u16 x x坐标
 * @retvalue   :None
******************************************************************************/ 
void Set_X(u16 x)
{
	oled_info.OLED_X = x;
}
/*****************************************************************************
 * @name       :void Set_Y(u16 y)
 * @date       :2021.6.6 
 * @function   :设置y初始
 * @param 		 :u16 y y坐标
 * @retvalue   :None
******************************************************************************/ 
void Set_Y(u16 y)
{
	oled_info.OLED_Y = y;
}
/*****************************************************************************
 * @name       :void Set_X_Y(u16 x,u16 y)
 * @date       :2021.6.6 
 * @function   :设置x,y初始位置
 * @param 		 :u16 x x坐标
 * @param 		 :u16 y y坐标
 * @retvalue   :None
******************************************************************************/ 
void Set_X_Y(u16 x,u16 y)
{
	oled_info.OLED_X = x;
	oled_info.OLED_Y = y;
}
/*****************************************************************************
 * @name       :void OLED_GPIOInit(void)
 * @date       :2021.6.6 
 * @function   :OLED引脚的初始化
 * @param 		 :None
 * @retvalue   :None
******************************************************************************/
void OLED_GPIOInit(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStruct.GPIO_Pin = OLED_RES|OLED_CS|OLED_BLK|OLED_DC;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	GPIO_SetBits(GPIOB,OLED_CS|OLED_RES|OLED_BLK);
	GPIO_ResetBits(GPIOB,OLED_DC);
}
/*****************************************************************************
 * @name       :void OLED_ReSet(void)
 * @date       :2021.6.6 
 * @function   :OLED的重置
 * @param 		 :None
 * @retvalue   :None
******************************************************************************/
void OLED_ReSet(void)
{
	OLED_RES_ENABLE;
	delay_ms(100);
	OLED_RES_DISABLE;
	delay_ms(100);
}
/*****************************************************************************
 * @name       :void OLED_WriteCmd(u16 cmd)
 * @date       :2021.6.6 
 * @function   :OLED发送16位的命令
 * @param 		 :u8 cmd	命令
 * @retvalue   :None
******************************************************************************/
void OLED_WriteCmd(u16 cmd)
{
	OLED_CS_ENABLE;
	OLED_instruction;
	SPIx_WriteByte(SPI1,cmd);
	OLED_CS_DISABLE;
}
/*****************************************************************************
 * @name       :void OLED_WriteData(u8 data)
 * @date       :2021.6.6 
 * @function   :OLED发送16位的数据
 * @param 		 :u8 data	数据
 * @retvalue   :None
******************************************************************************/
void OLED_WriteData(u8 data)
{
	OLED_CS_ENABLE;
	OLED_data;
	SPIx_WriteByte(SPI1,data);
	OLED_CS_DISABLE;
}
/*****************************************************************************
 * @name       :void OLED_WriteData16Bit(u16 data)
 * @date       :2021.6.6 
 * @function   :OLED发送16位的数据
 * @param 		 :u16 data	数据
 * @retvalue   :None
******************************************************************************/
void OLED_WriteData16Bit(u16 data)
{
	OLED_CS_ENABLE;
	OLED_data;
	SPIx_WriteByte(SPI1,data>>8);
	SPIx_WriteByte(SPI1,data);
	OLED_CS_DISABLE;
}

void OLED_WriteReg(u8 OLED_Reg, u16 OLED_RegValue)
{
	OLED_WriteCmd(OLED_Reg);  
	OLED_WriteData(OLED_RegValue);
}

void OLED_WriteGRAM(void)
{
	OLED_WriteCmd(OLED_WriteGRAMCmd);
}

void OLED_DisplayConifig(According Angle)
{
	switch(Angle)
	{
		case OLED_flip0:
				oled_info.OLED_Hight = OLED_W;
				oled_info.OLED_Width = OLED_H;
				Set_X_Y(2,3);
				//Set_X_Y(20,16);
//				OLED_WriteCmd(0x36); //方向选择
//				OLED_WriteData((1<<3)|(1<<6)|(1<<7));
				OLED_WriteReg(0x36,(1<<3)|(1<<6)|(1<<7));	//BGR==1,MY==0,MX==0,MV==0(从左到右,从上到下)
		break;
		case OLED_flip90:
				oled_info.OLED_Hight = OLED_H;
				oled_info.OLED_Width = OLED_W;
				Set_X_Y(3,2);
//				OLED_WriteCmd(0x36); //方向选择
//				OLED_WriteData((1<<3)|(1<<7)|(1<<5));
				OLED_WriteReg(0x36,(1<<3)|(1<<7)|(1<<5));	//BGR==1,MY==1,MX==1,MV==0(从右到左,从上到下)
		break;
		case OLED_flip180:
				oled_info.OLED_Hight = OLED_W;
				oled_info.OLED_Width = OLED_H;
				Set_X_Y(2,1);
//				OLED_WriteCmd(0x36); //方向选择
//				OLED_WriteData((1<<3)|(0<<6)|(0<<7));
				OLED_WriteReg(0x36,(1<<3)|(0<<6)|(0<<7));	//BGR==1,MY==0,MX==1,MV==1(从左到右,从下到上)
		break;
		case OLED_flip270:
				oled_info.OLED_Hight = OLED_H;
				oled_info.OLED_Width = OLED_W;
				Set_X_Y(1,2);
//				OLED_WriteCmd(0x36); //方向选择
//				OLED_WriteData((1<<3)|(0<<7)|(1<<6)|(1<<5));
				OLED_WriteReg(0x36,(1<<3)|(0<<7)|(1<<6)|(1<<5));	//BGR==1,MY==1,MX==0,MV==0(从下到上,从左到右)
		break;
	}
}
/*****************************************************************************
 * @name       :void OLED_Init(void)
 * @date       :2021.6.6 
 * @function   :OLED的初始化
 * @param 		 :None
 * @retvalue   :None
******************************************************************************/
void OLED_Init(void)
{
	SPIx_Init();
	SPIx_SetSpeed(SPI1,SPI_Speed_high);
	OLED_GPIOInit();
	OLED_ReSet();		//<LCD初始化前复位 Reset before LCD Init.
	
	oled_info.OLED_Hight = OLED_H;
	oled_info.OLED_Width = OLED_W;
	oled_info.OLED_X = 0;
	oled_info.OLED_Y = 0;
	oled_info.OLED_Der = OLED_flip0;
	
	FRONT_COLOR = WHITE;
	BACK_COLOR = BLACK;
	
	//LCD Init For 1.44Inch LCD Panel with ST7735R.
	OLED_WriteCmd(0x11);//Sleep exit 退出睡眠
	delay_ms (120);
		
	//ST7735R Frame Rate
	OLED_WriteCmd(0xB1); 			//帧速率控制(正常模式/全色)
	OLED_WriteData(0x01); 
	OLED_WriteData(0x2C); 
	OLED_WriteData(0x2D);
	
	OLED_WriteCmd(0xB2); 			//帧率控制(空闲模式/ 8色)
	OLED_WriteData(0x01); 
	OLED_WriteData(0x2C); 
	OLED_WriteData(0x2D); 
	
	OLED_WriteCmd(0xB3); 			//帧速率控制(局部模式/全色模式)
	OLED_WriteData(0x01); 
	OLED_WriteData(0x2C); 
	OLED_WriteData(0x2D); 
	OLED_WriteData(0x01); 
	OLED_WriteData(0x2C); 
	OLED_WriteData(0x2D);
 	
	OLED_WriteCmd(0xB4); //Column inversion 	显示反转控制
	OLED_WriteData(0x07);
 	
	//ST7735R Power Sequence
	OLED_WriteCmd(0xC0); 		//功率控制1 	
	OLED_WriteData(0xA2); 
	OLED_WriteData(0x02); 
	OLED_WriteData(0x84);
	
	OLED_WriteCmd(0xC1); 		//功率控制2
	OLED_WriteData(0xC5);
	
	OLED_WriteCmd(0xC2); 		//电源控制3(正常模式/全色模式)
	OLED_WriteData(0x0A); 
	OLED_WriteData(0x00);
	
	OLED_WriteCmd(0xC3); 			//电源控制4(空闲模式/8模式)
	OLED_WriteData(0x8A); 
	OLED_WriteData(0x2A);
	
	OLED_WriteCmd(0xC4); 			//电源控制5(局部模式/全色模式)
	OLED_WriteData(0x8A); 
	OLED_WriteData(0xEE); 
	
	OLED_WriteCmd(0xC5); //VCOM 
	OLED_WriteData(0x0E);
 	
	OLED_WriteCmd(0x36); //MX, MY, RGB mode 
	OLED_WriteData(0xC8); 
	
	//ST7735R Gamma Sequence
	OLED_WriteCmd(0xe0); 		//伽玛(' + '极性)校正特性设置
	OLED_WriteData(0x0f); 
	OLED_WriteData(0x1a); 
	OLED_WriteData(0x0f); 
	OLED_WriteData(0x18); 
	OLED_WriteData(0x2f); 
	OLED_WriteData(0x28); 
	OLED_WriteData(0x20); 
	OLED_WriteData(0x22); 
	OLED_WriteData(0x1f); 
	OLED_WriteData(0x1b); 
	OLED_WriteData(0x23); 
	OLED_WriteData(0x37); 
	OLED_WriteData(0x00); 	
	OLED_WriteData(0x07); 
	OLED_WriteData(0x02); 
	OLED_WriteData(0x10);
	
	OLED_WriteCmd(0xe1); 		//伽玛(' - '极性)校正特性设置
	OLED_WriteData(0x0f); 
	OLED_WriteData(0x1b); 
	OLED_WriteData(0x0f); 
	OLED_WriteData(0x17); 
	OLED_WriteData(0x33); 
	OLED_WriteData(0x2c); 
	OLED_WriteData(0x29); 
	OLED_WriteData(0x2e); 
	OLED_WriteData(0x30); 
	OLED_WriteData(0x30); 
	OLED_WriteData(0x39); 
	OLED_WriteData(0x3f); 
	OLED_WriteData(0x00); 
	OLED_WriteData(0x07); 
	OLED_WriteData(0x03); 
	OLED_WriteData(0x10); 
 	
	OLED_WriteCmd(0x2a);			//x坐标
	OLED_WriteData(0x00);
	OLED_WriteData(0x00);
	OLED_WriteData(0x00);
	OLED_WriteData(0x7f);
	
	OLED_WriteCmd(0x2b);		//y坐标
	OLED_WriteData(0x00);
	OLED_WriteData(0x00);
	OLED_WriteData(0x00);
	OLED_WriteData(0x9f);
	
	OLED_WriteCmd(0xF0); //Enable test command  		使能测试命令
	OLED_WriteData(0x01);
	
	OLED_WriteCmd(0xF6); //Disable ram power save mode 		禁用ram电源保存模式
	OLED_WriteData(0x00); 	
	
	OLED_WriteCmd(0x3A); //65k mode 		65 k模式
	OLED_WriteData(0x05); 
	
	OLED_WriteCmd(0x29);//Display on		显示打开
	
	OLED_DisplayConifig(oled_info.OLED_Der);//设置LCD显示方向
	OLED_BLK_DISABLE;//点亮背光		 
	OLED_Clear(WHITE);//清全屏白色
}
/*****************************************************************************
 * @name       :void OLED_Init(void)
 * @date       :2021.6.6 
 * @function   :OLED窗口设置
 * @param 		 :u8 startx	开始x坐标
 * @param 		 :u8 starty	开始y坐标
 * @param 		 :u8 endx	结束x坐标
 * @param 		 :u8 endy	结束y坐标
 * @retvalue   :None
******************************************************************************/
void OLED_SetWindows(u8 startx,u8 starty,u8 endx,u8 endy)
{
	OLED_WriteCmd(OLED_SetXCmd);
	OLED_WriteData(OLED_NOP);
	OLED_WriteData(startx);		//
	OLED_WriteData(OLED_NOP);
	OLED_WriteData(endx+oled_info.OLED_X);
	
	OLED_WriteCmd(OLED_SetYCmd);
	OLED_WriteData(OLED_NOP);
	OLED_WriteData(starty);		//
	OLED_WriteData(OLED_NOP);
	OLED_WriteData(endy+oled_info.OLED_Y);
	
	OLED_WriteGRAM();
	
}
/*****************************************************************************
 * @name       :void OLED_Clear(u16 color)
 * @date       :2021.6.6 
 * @function   :OLED清屏
 * @param 		 :u16 color 颜色数据
 * @retvalue   :None
******************************************************************************/
void OLED_Clear(u16 color)
{
	u8 i,m;
	OLED_SetWindows(0,0,oled_info.OLED_Hight - 1,oled_info.OLED_Width - 1);
	OLED_CS_ENABLE;
	OLED_data;
	for(i=0;i<oled_info.OLED_Hight;i++)
	{
    for(m=0;m<oled_info.OLED_Width;m++)
    {	
			OLED_WriteData16Bit(color);
		}
	}
	OLED_CS_DISABLE;
}

/*****************************************************************************
 * @name       :void OLED_Cursor(u8 x,u8 y)
 * @date       :2021.6.6 
 * @function   :设置坐标
 * @param 		 :u8 x		x的坐标
 * @param 		 :u8 y		y的坐标
 * @retvalue   :None
******************************************************************************/
void OLED_Cursor(u8 x,u8 y)
{
	OLED_SetWindows(x,y+1,x,y+1);
}

/*****************************************************************************
 * @name       :void OLED_SetDraw(u8 x,u8 y)
 * @date       :2021.6.6 
 * @function   :快速画点
 * @param 		 :u8 x		x的坐标
 * @param 		 :u8 y		y的坐标
 * @retvalue   :None
******************************************************************************/
void OLED_SetDraw(u8 x,u8 y)
{
	OLED_Cursor(x,y);
	OLED_WriteData16Bit(FRONT_COLOR);
}
/*****************************************************************************
 * @name       :void OLED_SetDraw(u8 x,u8 y)
 * @date       :2021.6.6 
 * @function   :指定颜色画点
 * @param 		 :u8 x		x的坐标
 * @param 		 :u8 y		y的坐标
 * @param 		 :u16 color 颜色数据
 * @retvalue   :None
******************************************************************************/
void OLED_SetColorDraw(u8 x,u8 y,u16 color)
{
	OLED_Cursor(x,y);
	OLED_WriteData16Bit(color);
}
